package dmt16.TelemetrySuit.Controller.SensorTypes
{
	import dmt16.TelemetrySuit.Utils.NMEASentence;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;

	public class GPSModule implements ISensorType
	{
		public function GPSModule()
		{
			data=new Vector.<NMEASentence>();
		}
		
		public function get sensorType():int
		{
			return SensorType.GPS_MODULE;
		}
		
		public function get dataFormat():int
		{
			return DataFormat.String;
		}
		
		public var data:Vector.<NMEASentence>;
		
		public function addData(value:*,time:Number):void
		{
			if(value is String){
				var fields:Array=String(value).split(",");
				if(fields.length==11){
					var sentence:NMEASentence=new NMEASentence(value as String);
					data.push(sentence);
					_speedTotal+=sentence.speed();
					if(data.length>1&&data[data.length-1].dateTime!=data[data.length-2].dateTime)
						_distanceTotal+=data[data.length-1].speed();
					_eventManager.dispatchEvent(new SensorEvent(SensorEvent.SENSOR_DATA_EVENT));
				}
			}
		}
				
		private var _speedTotal:Number=0;
		public function get averageSpeed():Number
		{
			return _speedTotal/data.length;
		}
		
		private var _distanceTotal:Number=0;
		public function get totalDistance():Number{
			return _distanceTotal;
		}
		
		private var _eventManager:EventDispatcher=new EventDispatcher();
		public function addEventListener(type:String, listener:Function, useCapture:Boolean=false, priority:int=0, useWeakReference:Boolean=false):void
		{
			_eventManager.addEventListener(type,listener,useCapture,priority,useWeakReference);
		}
		public function removeEventListener(type:String, listener:Function, useCapture:Boolean=false):void{
			_eventManager.removeEventListener(type,listener,useCapture);
		}
		public function dispatchEvent(event:Event):Boolean{
			return _eventManager.dispatchEvent(event);
		}
		public function hasEventListener(type:String):Boolean{
			return _eventManager.hasEventListener(type);
		}
		public function willTrigger(type:String):Boolean{
			return _eventManager.willTrigger(type);
		}
	}
}